using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

/// <summary>
/// Summary description for Geographics
/// </summary>
public class Geographics
{
    public const int ANGLE_TO_MILE_SCALE = 100;

    static ZipDataSetTableAdapters.ZipsTableAdapter
        zipsTableAdapter = new ZipDataSetTableAdapters.ZipsTableAdapter();

    static Geographics()
    {
    }

    public static Location GetLocation(string ZipCode)
    {
        ZipDataSet.ZipsDataTable
            resultTable = zipsTableAdapter.GetData(ZipCode);
        if (resultTable.Count == 0)
            return new Location(double.PositiveInfinity, double.PositiveInfinity, null, null);
        else return new Location(resultTable[0].longitude,
            resultTable[0].latitude,
            resultTable[0].city,
            resultTable[0].state);
    }

    public static List<int[]> 
        getGridCoordinatesWithinRange(int Range, double longitude, double latitude)
    {
        List<int[]> locations = new List<int[]>();
        int[] loc = ConvertToMiles(longitude, latitude);
        int vertiMile = loc[0];
        int horiMile = loc[1];
        for (int i = -Range; i <= Range; i++)
        {
            //given x coordinate, computes y coordinate for a circle
            int scale = (int)Math.Sqrt((double)(Range * Range) - (double)(i * i));
            for (int j = -scale; j <= scale; j++)
            {
                locations.Add(new int[] { vertiMile + i, horiMile + j });
            }
        }
        return locations;
    }

    public static int[] ConvertToMiles(double longitude, double latitude)
    {
        int[] result = new int[2];
        result[0] = Convert.ToInt32(longitude * Geographics.ANGLE_TO_MILE_SCALE);
        result[1] = Convert.ToInt32(latitude * Geographics.ANGLE_TO_MILE_SCALE);
        return result;
    }

    public static int[] ConvertToMiles(Location location)
    {
        return ConvertToMiles(location.Longitude, location.Latitude);
    }
}

public struct Location
{
    public Location(double longitude, double latitude, string city, string state)
    {
        Longitude = longitude;
        Latitude = latitude;
        City = city;
        State = state;
    }

    public double Longitude;
    public double Latitude;
    public string City;
    public string State;
}